package br.com.advance.automacao.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import br.com.advance.automacao.dao.ErroDao;
import br.com.advance.automacao.generic.dao.impl.GenericDaoImpl;
import br.com.advance.automacao.model.Erro;

@Repository("erroDao")
public class ErroDaoImpl extends GenericDaoImpl<Erro, Serializable> implements ErroDao {

	@SuppressWarnings("unused")
	private static final Logger logger = Logger.getLogger(ErroDaoImpl.class);

	@Override
	public Erro buscarErroPorCodigoClp(Long idCLP, String codigo) {
		Query query = entityManager.createNamedQuery(Erro.BUSCAR_ERRO_POR_CLP_CODIGO);
		query.setParameter("idCLP", idCLP);
		query.setParameter("codigo", codigo);
		return (Erro) query.getSingleResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Erro> buscarErroPorCodigoClp(Long idCLP) {
		Query query = entityManager.createNamedQuery(Erro.BUSCAR_ERRO_POR_CLP);
		query.setParameter("idCLP", idCLP);
		return query.getResultList();
	}

	@Override
	public Erro buscarPorId(Long id) {
		Query query = entityManager.createNamedQuery(Erro.BUSCAR_ERRO_POR_ID);
		query.setParameter("id", id);
		return (Erro) query.getSingleResult();
	}

	@Override
	public Erro buscarPorCodigoCLP(String codigo, Long idCLP) {
		try {
			Query query = entityManager.createNamedQuery(Erro.BUSCAR_ERRO_POR_CODIGO_CLP);
			query.setParameter("codigo", codigo);
			query.setParameter("idCLP", idCLP);
			return (Erro) query.getSingleResult();
		} catch(NoResultException e) {
			return null;
		}

	}

}
